colorAdminApp.controller('faceAddController', function($scope,$compile,$rootScope,$http,$state,$stateParams,modalService) {
	
	$("#video").hide();
	$("#currentPhoto").hide();
	
	var mediaStreamTrack=null
	$scope.getMedia=function() {
		
		$("#video").show();
		$("#currentPhoto").hide();
		
        let constraints = {
            video: {width: 500, height: 500},
            audio: true
        };
        //获得video摄像头区域
        let video = document.getElementById("video");
        //这里介绍新的方法，返回一个 Promise对象
        // 这个Promise对象返回成功后的回调函数带一个 MediaStream 对象作为其参数
        // then()是Promise对象里的方法
        // then()方法是异步执行，当then()前的方法执行完后再执行then()内部的程序
        // 避免数据没有获取到
        let promise = navigator.mediaDevices.getUserMedia(constraints);
        promise.then(function (MediaStream) {
        	mediaStreamTrack=MediaStream.getVideoTracks()[0];
            video.srcObject = MediaStream;
            video.play();
        });
    }
	
	$scope.stopMedia=function() {
		 if(mediaStreamTrack==null){
			 return;
		 }
         mediaStreamTrack.stop();
    }

   $scope.getVideoFace=function() {
	  //获得Canvas对象
	 let video = document.getElementById("video");
	
	 var imageBase64=$scope.getImageBase64(video);
	 
	 imageBase64=$scope.replaceBase64(imageBase64);
	 
	 $scope.getFace(imageBase64);
	 
   }
   
   $scope.getPhotoFace=function() {
	   
		 var imageBase64=$("#currentPhoto").attr("src");
		 
		 imageBase64=$scope.replaceBase64(imageBase64);
		
		 $scope.getFace(imageBase64);
		 
    }
   
   $scope.replaceBase64=function(imageBase64){
	
	   var newImageBase64=imageBase64.replace("data:image/png;base64,","").replace("data:image/jpeg;base64,","").replace("data:image/jpg;base64,","");
     
	  return newImageBase64;
   
   }
   
   
   $scope.getFace=function(imageBase64){
	   $(".row-fluid").block({message:'正在执行操作，请稍等...'});
	   $http.post('api/pepper/face/detectMax',{params:{"imageBase64":imageBase64}}).success(function(data, status, headers, config) {
		   $(".row-fluid").unblock();
			modalService.showAlertModal('注意',data.message);
			if(data.status==1){
				var faceImageBase64="data:image/png;base64,"+data.data;
				$scope.addFace(faceImageBase64);
			}else{
				modalService.showAlertModal('注意',"操作失败，未采集到人脸！");
			}
		}).error(function(data, status, headers, config) {
			$(".row-fluid").unblock();
       });  
   }
   
   
   $scope.addFace=function(imageBase64){
	   $scope.faceList.push({"imgBase64":imageBase64});
   }
   

   
   $scope.getImageBase64=function(img){
       let canvas = document.createElement("canvas");
       canvas.width = img.width;
       canvas.height = img.height;
       var ctx = canvas.getContext("2d");
       ctx.drawImage(img, 0, 0, img.width, img.height);
       var dataURL = canvas.toDataURL("image/png");
       return dataURL;
       
      // return dataURL.replace("data:image/png;base64,","");
   }
	
   
   
   $scope.deleteFaceItem=function(deleteIndex){
       var items= $scope.faceList;
       for(var i=0;i<items.length;i++){
           if(deleteIndex==i){
               $scope.faceList.splice(i,1);
           }
       }
   };
   
   $scope.faceList=[];
   
 
   
   $scope.fileChanged = function(ele){  
	   
	    $("#video").hide();
	    
		$("#currentPhoto").show();
		
		$scope.stopMedia();
		
	    $scope.files = ele.files;
	    
	    var file = document.querySelector('input[type=file]').files[0]; 
	    
	    var filename = $scope.files[0].name; 
	   
	   if(filename.length> 1 && filename ) {  

	        var ldot = filename.lastIndexOf(".");  

	        var type = filename.substring(ldot + 1);  //文件类型
	        
	        if(type!="jpg"&&type!="png"&&type!="jpeg"){
	        	
	        	alert("请选择jpg、png、jpeg格式图片");
	        	
	        	return;
	        }
	        
		    var file = ele.files[0];//获取文件信息
		    
		    if (window.FileReader){
		        var reader = new FileReader();
		        reader.readAsDataURL(file);
		        //监听文件读取结束后事件    
		        reader.onloadend = function (e) {
		        	
		        	$("#currentPhoto").attr("src",e.target.result);
		            
		        };
		    }
	   }

	}
   
   $scope.save=function(entity){
	   
	   if($scope.faceList.length==0){
		   alert("请先采集人脸！");
		   return;
	   }
    	
    	entity["faceImageList"]= $scope.replaceBase64List($scope.faceList);
    	
    	var requestJson=JSON.stringify(entity);
    	
    	$("#requestJson").val(requestJson);
    	
   		$(".row-fluid").block({message:'正在执行操作，请稍等...'});
   		
		$http.post('api/pepper/face/add',{params:{"requestJson":requestJson}}).success(function(data, status, headers, config) {
			$(".row-fluid").unblock();
			modalService.showAlertModal('注意',data.message);
			if(data.status==1){
				$scope.loadData();
			}
		}).error(function(data, status, headers, config) {
			$(".row-fluid").unblock();
        });  
	};
	
	 $scope.replaceBase64List=function(faceList){
		 var retVal=[];
		 for(var i=0;i<faceList.length;i++){
			 var newBase64=$scope.replaceBase64(faceList[i].imgBase64);
			 retVal.push({"imgBase64":newBase64});
		 }
		 return retVal;
	 }
	 
	 
	   $scope.addFromFoler=function(){

		   $(".row-fluid").block({message:'正在执行操作，请稍等...'});
	   		
			$http.post('api/pepper/face/addFromFoler',{params:{}}).success(function(data, status, headers, config) {
				$(".row-fluid").unblock();
				modalService.showAlertModal('注意',data.message);
			
			}).error(function(data, status, headers, config) {
				$(".row-fluid").unblock();
	        });  
		};
	 
});
